package shop.product.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import shop.common.core.Result;
import shop.common.entity.Product;
import shop.product.service.ProductService;

import java.util.List;

@RestController
@RequestMapping("/api/products")
@RequiredArgsConstructor
public class ProductController {

    private final ProductService productService;

    @GetMapping("/test")
    public Result test() {
        return Result.ok().data("test");
    }

    @PostMapping("/create")
    public Result create(@RequestBody Product product) {
        boolean save = productService.save(product);
        return save ? Result.ok() : Result.fail();
    }


    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable("id") String id) {
        boolean flag = productService.removeById(id);
        return flag ? Result.ok() : Result.fail();
    }

    @PutMapping("/update")
    public Result update(@RequestBody Product product) {
        boolean bool = productService.updateById(product);
        return bool ? Result.ok() : Result.fail();
    }


    @GetMapping("/details/{id}")
    public Result details(@PathVariable String id) {
        Product product = productService.getById(id);
        return Result.ok().data(product);
    }


    @PostMapping("/list")
    public Result page(@RequestBody Product product, @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        Page<Product> page = productService.list(product, pageNum, pageSize);
        return Result.ok().data(page);
    }

    @GetMapping("/listByName")
    public Result list(@RequestParam String name) {
        List<Product> list = productService.listByName(name);
        return Result.ok().data(list);
    }
}
